#include "KBufferDC.h"

KBufferDC::KBufferDC():
m_hMemDC(NULL),
m_hDesDC(NULL),
m_hBitmap(NULL),
m_hOldBitmap(NULL),
m_nWidth(0),
m_nHeight(0),
m_hBKBrush(NULL)
{
}

KBufferDC::~KBufferDC()
{
	if(m_hBKBrush != NULL)
		DeleteObject(m_hBKBrush);

	if(m_hBitmap != NULL)
		DeleteObject(m_hBitmap);

	if(m_hMemDC == NULL)
		DeleteObject(m_hMemDC);
}


bool KBufferDC::Create(HWND hDesWnd, COLORREF colorBK)
{
	RECT rect;
	GetClientRect(hDesWnd, &rect);
	m_nWidth = rect.right - rect.left;
	m_nHeight = rect.bottom - rect.top;

	m_hDesDC = GetDC(hDesWnd);
	m_hMemDC = CreateCompatibleDC(NULL);
	m_hBitmap = CreateCompatibleBitmap(m_hDesDC, m_nWidth, m_nHeight);

	m_hBKBrush = ::CreateSolidBrush(colorBK);

	return (m_hBitmap != NULL);
}

void KBufferDC::BeginDraw()
{
	m_hOldBitmap = (HBITMAP)SelectObject(m_hMemDC,  m_hBitmap);
	RECT rect = {0, 0, m_nWidth, m_nHeight};
	FillRect(m_hMemDC, &rect, m_hBKBrush);
}
void KBufferDC::EndDraw()
{
	::BitBlt(m_hDesDC, 0, 0, m_nWidth, m_nHeight, m_hMemDC, 0, 0, SRCCOPY);
	::SelectObject(m_hMemDC, m_hOldBitmap);
}

HDC	KBufferDC::GetMemDC()
{
	return m_hMemDC;
}

int KBufferDC::GetWidth()
{
	return m_nWidth;
}

int KBufferDC::GetHeight()
{
	return m_nHeight;
}